package com.bit.bit_forum.service.impl;

import com.bit.bit_forum.common.AppResult;
import com.bit.bit_forum.common.ResultCode;
import com.bit.bit_forum.dao.BoardMapper;
import com.bit.bit_forum.dao.UserMapper;
import com.bit.bit_forum.exception.ApplicationException;
import com.bit.bit_forum.model.Board;
import com.bit.bit_forum.service.IBoardService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;

    @Override
//<!--    获取未被删除的板块-->
    public List<Board> selectByNum(Integer n) {
            //参数校验
        if(n<0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return boardMapper.selectByNum(n);
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if(id==null || id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.warn(ResultCode.FAILED.toString()+" board为null");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        int n = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(n!=1){
            log.warn(ResultCode.FAILED.toString()+" 受影响的行数不是1. ");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        if(id==null || id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.warn(ResultCode.FAILED.toString()+" board为null");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if(updateBoard.getArticleCount()<0){
            updateBoard.setArticleCount(0);
        }
        int n = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(n!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Board getById(Long id) {
        if(id==null || id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return board;
    }
}
